package com.gc.server.controller.api;

import com.gc.server.entity.*;
import com.gc.server.enums.BusApplyLeaveTypeEnum;
import com.gc.server.enums.BusApplyStatusEnum;
import com.gc.server.enums.LevelEnum;
import com.gc.server.service.*;
import com.gc.server.vo.*;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.tz.common.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/apply")
public class ApiApplyController {

    @Autowired
    private GoodsApplyService goodsApplyService;

    @Autowired
    private ApplyItemService applyItemService;

    @Autowired
    private BusApplyService busApplyService;

    @PostMapping("/goods")
    public R goods(@LoginUserId Long userId, Long[] ids, Integer[] nums, String remark, String address, String unit) {
        goodsApplyService.apply(userId, ids, nums, remark, address, unit);
        return R.ok();
    }

    @GetMapping("/goods/{id}")
    public R goods(@PathVariable Long id) {
        GoodsApplyEntity goodsApplyEntity = goodsApplyService.get(id);
        List<ApplyItemEntity> items = applyItemService.findByApplyId(id);
        return R.ok(goodsApplyEntity).putExtra("items", items);
    }

    /**
     * 获取总价
     *
     * @param userId
     * @return
     */
    @GetMapping("/goods/sum")
    public R goodsSum(@LoginUserId Long userId) {
        BigDecimal amount = goodsApplyService.sumAmount(new Date(), userId);

        return R.ok(amount);
    }

    @GetMapping("/goods/sum/check")
    public R goodsSumCheck(@LoginUserId Long userId) {
        //获取待审核
        Integer checkSum = goodsApplyService.sumCheck(userId);
        return R.ok(checkSum);
    }

    /**
     * 所有的物料申请
     *
     * @return
     */
    @GetMapping("/goods")
    public R goodsList(@LoginUserId Long userId, Boolean top, Date startDate, Date endDate, Long cid) {
        List<GoodsApplyVo> goodsApplyVos = null;
        BigDecimal amount = null;
        if (top) {
            if (cid == null) {
                goodsApplyVos = goodsApplyService.findByPid(userId, startDate, endDate);
                amount = goodsApplyService.sumAmountByPid(new Date(), userId);
            } else {
                goodsApplyVos = goodsApplyService.findByUserId(cid, startDate, endDate);
                amount = goodsApplyService.sumAmount(new Date(), cid);
            }
        } else {
            goodsApplyVos = goodsApplyService.findByUserId(userId, startDate, endDate);
            amount = goodsApplyService.sumAmount(new Date(), userId);
        }
        return R.ok(goodsApplyVos).putExtra("amount", amount);
    }

    /**
     * 出差申请
     *
     * @param userId
     * @param type
     * @param info
     * @param startDate
     * @param endDate
     * @return
     */
    @PostMapping("/bus")
    public R bus(@LoginUserId Long userId, @RequestBody ApplyParam param) {
        ApplyEntity applyEntity = busApplyService.apply(userId, param.getInfo(), param.getItems());
        return R.ok(applyEntity);
    }

    /**
     * 请假
     *
     * @return
     */
    @PostMapping("/bus/leave")
    public R busLeave(@LoginUserId Long userId, Integer leaveType, String info, Date startDate, Date endDate, BigDecimal hours) {
        BusApplyEntity busApplyEntity = busApplyService.applyLeave(userId, info, startDate, endDate, 0, leaveType, hours);
        return R.ok(busApplyEntity);
    }

    /**
     * 获取出差
     *
     * @param userId
     * @param id
     * @return
     */
    @GetMapping("/bus/{id}")
    public R bus(@LoginUserId Long userId, @PathVariable Long id) {
        ApplyEntity applyEntity = applyService.get(id);
        List<BusApplyEntity> items = busApplyService.findByAid(applyEntity.getId());
        applyEntity.setItems(items);
        //获取兑费 总监以及以上80一天  以下50一天
        UserEntity user = userService.get(applyEntity.getUserId());
        LevelEntity level = levelService.get(user.getLevelId());
        //从字典获取兑费
        List<DicTypeEntity> dicType = dicTypeService.findByName(level.getName());
        BigDecimal amount = BigDecimal.ZERO;
        if(dicType.size()>0){
            amount = new BigDecimal(dicType.get(0).getLabel());
        }
//        if(level.getLv()>=5){
//            amount = new BigDecimal(80);
//        }
        //取整数
        BigDecimal hour = applyEntity.getHours().divideAndRemainder(new BigDecimal(9))[0];
        //取余数
        BigDecimal lit = applyEntity.getHours().divideAndRemainder(new BigDecimal(9))[1];
        BigDecimal sum = hour.multiply(amount);
        //有余数 加半天
        if(lit.compareTo(BigDecimal.ZERO)>0){
          sum = sum.add(amount.divide(new BigDecimal(2)));
        }
       CostApplyEntity costApply = costApplyService.findByBusId(id);
        return R.ok(applyEntity).putExtra("money",sum).putExtra("costApply",costApply);
    }

    @Autowired
    private DicTypeService dicTypeService;



    @Autowired
    private ApplyService applyService;

    /**
     * 获取请假
     *
     * @param userId
     * @param id
     * @return
     */
    @GetMapping("/bus/leave/{id}")
    public R busLeave(@LoginUserId Long userId, @PathVariable Long id) {
        BusApplyEntity busApplyEntity = busApplyService.get(id);
        return R.ok(busApplyEntity);
    }

    //计算时间
    @GetMapping("/bus/hour")
    public R calHours(Date start, Date end) {
        BigDecimal hours = busApplyService.calHours(start, end);
        return R.ok(hours);
    }

    /**
     * 审核出差
     *
     * @param userId
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/bus/{id}")
    public R bus(@LoginUserId Long userId, @PathVariable Long id, Integer status, String remark) {
        ApplyEntity applyEntity = applyService.check(userId, id, status, remark);
        return R.ok(applyEntity);
    }

    /**
     * 审核请假
     *
     * @param userId
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/bus/leave/{id}")
    public R busLeave(@LoginUserId Long userId, @PathVariable Long id, Integer status, String remark) {
        BusApplyEntity busApplyEntity = busApplyService.check(userId, id, status, remark);
        return R.ok(busApplyEntity);
    }


    @Autowired
    private UserService userService;

    @Autowired
    private LevelService levelService;

    /**
     * 找出所有出差的申请
     *
     * @param userId
     * @return
     */
    @GetMapping("/bus")
    public R busByTop(@LoginUserId Long topUserId, boolean top, Date startDate, Date endDate, Long userId) {
        List<ApplyVo> applyUserVos;
        if (top == true) { // 如果是审核
            applyUserVos = applyService.findMyShopUserAndDate(topUserId, userId, startDate, endDate);
        }else {
            applyUserVos = applyService.findByUserAndDate(topUserId, startDate, endDate);
        }
        return R.ok(applyUserVos);
    }

    /**
     * 找出所有请假的申请
     *
     * @param userId
     * @return
     */
    @GetMapping("/bus/leave")
    public R busLeaveByTop(@LoginUserId Long topUserId, boolean top, Date startDate, Date endDate, Long userId) {
        UserEntity userEntity = userService.get(topUserId);
        LevelEntity levelEntity = levelService.get(userEntity.getLevelId());
        List<ApplyUserVo> applyUserVos;
            if (top == true) { // 如果是审核
                applyUserVos = busApplyService.findMyShopUserAndDate(topUserId, userId, startDate, endDate);
            }else {
                applyUserVos = busApplyService.findByUserAndDate(topUserId, startDate, endDate);
            }
            return R.ok(applyUserVos);
    }

    /**
     * 统计出差需要审核的
     *
     * @param userId
     * @return
     */
    @GetMapping("/bus/sum")
    public R sumBus(@LoginUserId Long userId) {
        Integer count = applyService.sumCheck(userId);
        return R.ok(count);
    }
    /**
     * 统计请假审核的
     *
     * @param userId
     * @return
     */
    @GetMapping("/bus/sum/leave")
    public R sumBusLeave(@LoginUserId Long userId, Integer type) {
        Integer count = busApplyService.sumCheck(userId, type);
        return R.ok(count);
    }

    @GetMapping("/cost/check/sum")
    public R sumCostCheck(@LoginUserId Long userId) {
        Integer count = costApplyService.sumCheck(userId);
        return R.ok(count);
    }


    @Autowired
    private CostApplyService costApplyService;


    @PostMapping("/cost")
    public R cost(@LoginUserId Long userId, Integer type, BigDecimal amount, String bankInfo, String info, String img, String title, String unit, String payWay, Date payTime,Long busId,Long id,BigDecimal carMoney,BigDecimal houseMoney,BigDecimal otherMoney,BigDecimal moreMoney) {
        CostApplyEntity costApplyEntity = costApplyService.apply(userId, type, amount, bankInfo, info, img, title, unit, payWay, payTime,busId,id,carMoney,houseMoney,otherMoney,moreMoney);
        return R.ok(costApplyEntity);
    }


    /**
     * 获取
     *
     * @param userId
     * @param id
     * @return
     */
    @GetMapping("/cost/{id}")
    public R cost(@LoginUserId Long userId, @PathVariable Long id) {
        CostApplyEntity costApplyEntity = costApplyService.get(id);
        return R.ok(costApplyEntity);
    }


    @GetMapping("/cost/sum")
    public R sumCost(@LoginUserId Long userId) {
//        //查询所有卡号
        List<CostApplyVo> list = costApplyService.findByUser(userId, null, null);
        BigDecimal amount = BigDecimal.ZERO;
        List<String> cardInfos = new ArrayList<>();
        for (CostApplyVo costApplyVo : list) {
            if (costApplyVo.getEntity().getTime().getYear() == new Date().getYear()) {
                amount = amount.add(costApplyVo.getEntity().getAmount());
            }
            if (costApplyVo.getEntity().getBankInfo() != null && !cardInfos.contains(costApplyVo.getEntity().getBankInfo().trim())) {
                cardInfos.add(costApplyVo.getEntity().getBankInfo().trim());
            }
        }
        UserEntity user = userService.get(userId);
        String bankInfo = user.getBankName()+" "+ user.getBankBranch() + " " +user.getBankUser() + " " + user.getBankNum();
        return R.ok().putExtra("amount", amount).putExtra("cardInfos", cardInfos).putExtra("bankInfo",bankInfo);
    }

    /**
     * 审核
     *
     * @param userId
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/cost/{id}")
    public R cost(@LoginUserId Long userId, @PathVariable Long id, Integer status, String remark) {
        CostApplyEntity costApplyEntity = costApplyService.check(userId, id, status, remark);
        return R.ok(costApplyEntity);
    }

    /**
     * 审核
     *
     * @param userId
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/goods/{id}")
    public R goods(@LoginUserId Long userId, @PathVariable Long id, Integer status, String remark) {
        GoodsApplyEntity goodsApplyEntity = goodsApplyService.check(userId, id, status, remark);
        return R.ok(goodsApplyEntity);
    }


    /**
     * 找出所有业务的申请
     *
     * @param userId
     * @return
     */
    @GetMapping("/cost")
    public R costByTop(@LoginUserId Long userId, boolean top, Date startDate, Date endDate, Long cid) {
        List<CostApplyVo> applyUserVos;
        BigDecimal amount = BigDecimal.ZERO;
        if (top == true) { // 如果是审核
            if (cid != null) { // 如果没有cid
                applyUserVos = costApplyService.findByUser(cid, startDate, endDate);
                //查询本年度的金额
                amount = costApplyService.sumAmount(new Date(), cid);
            } else {
                applyUserVos = costApplyService.findByPidAndDate(userId, startDate, endDate);
                //查询本年度的金额
                amount = costApplyService.sumAmountByTop(new Date(), userId);
            }
        } else {
            applyUserVos = costApplyService.findByUser(userId, startDate, endDate);
            //查询本年度的金额
            amount = costApplyService.sumAmount(new Date(), userId);
        }
        return R.ok(applyUserVos).putExtra("amount", amount);
    }


    @Autowired
    private AdviseService adviseService;


    @PostMapping("/advise")
    public R advise(@LoginUserId Long userId, String title, String content, String img) {
        AdviseEntity adviseEntity = adviseService.apply(userId, title, content, img);
        return R.ok(adviseEntity);
    }


    /**
     * 获取
     *
     * @param userId
     * @param id
     * @return
     */
    @GetMapping("/advise/{id}")
    public R advise(@LoginUserId Long userId, @PathVariable Long id) {
        AdviseEntity adviseEntity = adviseService.get(id);
        return R.ok(adviseEntity);
    }


    /**
     * 找出所有业务的申请
     *
     * @param userId
     * @return
     */
    @GetMapping("/advise")
    public R adviseByTop(@LoginUserId Long userId) {
        List<AdviseVo> adviseVos = adviseService.findByUserId(userId);
        return R.ok(adviseVos);
    }


}
